<?php 

class Histats extends Module
{
	
	private $_postErrors = array();

	function __construct()
	{
		$this->name = 'histats';
		$this->tab = 'front_office_features';
		$this->version = 0.2;

		parent::__construct(); 

		$this->page = basename(__FILE__, '.php');
		$this->displayName = $this->l('Histats by heogia@gmail.com');
		$this->description = $this->l('Displays Histats in your homepage - More in http://email24h.net');		
	}

	function install()
	{
		if (!parent::install())
			return false;
		if (!$this->registerHook('leftColumn') || !$this->registerHook('footer'))
			return false;

		if (!Configuration::updateValue('HISTATS_LINK', ""))
			return false;
			
		if (!Configuration::updateValue('ALEXA_LINK', ""))
			return false;			
			
		return true;
	}
	
	public function uninstall()
    {
  		if(!parent::uninstall())
	    	return false;
		if(!Configuration::deleteByName('HISTATS_LINK')) 				
		    return false;
			
		if(!Configuration::deleteByName('ALEXA_LINK')) 				
		    return false;		
						
    	return true;
    }
	

	public function getContent()
	{	
	
		$this->_html = '<h2>'.$this->displayName.'</h2>';
		if (isset($_POST['submitHistats']))
		{
			$hi_link = urlencode(Tools::getValue('hi_link'));			
					
			Configuration::updateValue('HISTATS_LINK', $hi_link);
			
			$alexa_link = urlencode(Tools::getValue('alexa_link'));
					
			Configuration::updateValue('ALEXA_LINK', $alexa_link);			

			$this->displayConf();		
		}

     	$this->displayForm();

        return $this->_html;	
		
	}
	
	public function displayConf()
	{
		$this->_html .= '
		<div class="conf confirm">
			<img src="../img/admin/ok.gif" alt="'.$this->l('Confirmation').'" />
			'.$this->l('Settings updated').'
		</div>';
	}		

	public function displayForm()
	{		
		$conf = Configuration::getMultiple(array('HISTATS_LINK', 'ALEXA_LINK'));
			
		$hi_link = array_key_exists('hi_link', $_POST) ? $_POST['hi_link'] : (array_key_exists('HISTATS_LINK', $conf) ? $conf['HISTATS_LINK'] : '');	
		
		$alexa_link = array_key_exists('alexa_link', $_POST) ? $_POST['alexa_link'] : (array_key_exists('ALEXA_LINK', $conf) ? $conf['ALEXA_LINK'] : '');					
	
	
	 	$this->_html .= '
		<fieldset class="space">
			<legend><img src="'.$this->_path.'logo.gif" alt="" title="" /> '.$this->l('Settings').'</legend>
			<form method="post" action="'.$_SERVER['REQUEST_URI'].'">
			<div>
				<label>'.$this->l('HiStats Javascript Code:').'</label>				
				<textarea cols="60" rows="7" name="hi_link" id="hi_link">'.urldecode(Tools::getValue('hi_link', Configuration::get('HISTATS_LINK'))).'</textarea>	
			</div>
			</br>
			<div>	
				<label>'.$this->l('Alexa Javascript Code:').'</label>				
				<textarea cols="60" rows="7" name="alexa_link" id="alexa_link">'.urldecode(Tools::getValue('alexa_link', Configuration::get('ALEXA_LINK'))).'</textarea>		
			</div>																															
				
			<div class="margin-form"><input type="submit" class="button" name="submitHistats" value="'.$this->l('Update').'" /></div>
			</form>
		</fieldset>		';	
	}


    function hookLeftColumn($params)
	{
		return $this->hookRightColumn($params);
	}
	
	function hookFooter($params)
	{
		return $this->hookRightColumn($params);
	}
	
	function hookRightColumn($params)
	{
	
		global $smarty;

		$hi_link = urldecode(Configuration::get('HISTATS_LINK'));			
		$alexa_link = urldecode(Configuration::get('ALEXA_LINK'));			
		
		$smarty->assign(array(
	
		'hi_links' => $hi_link,
		'alexa_links' => $alexa_link,		
			
		));		 	
	
		
		return $this->display(__FILE__, 'histats.tpl');	
	}		
	

}